﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace EmployeeCheckIn
{
    public partial class ShowReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null && Session["Pass"] != null)
                {
                    if (Session["GridViewDataSource"] != null)
                    {
                        DataTable DataGridview = (DataTable)Session["GridViewDataSource"];
                        GridView2.Visible = false;
                        if (DataGridview.Rows.Count == 0)
                        {
                            lb_warn.Visible = true;
                        }
                        else
                        {
                            GridView2.Visible = true;
                            lb_warn.Visible = false;
                            GridView2.DataSource = DataGridview;
                            GridView2.DataBind();
                        }
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
        private void GridView2Excal(DataTable dt)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView2.DataSource = dt;
            //GridView2.Columns[GridView2.HeaderRow.Cells.Count-1].Visible = false;
            GridView2.DataBind();
            GridView2.AllowPaging = false;
            GridView2.HeaderRow.Style.Add("background-color", "#FFFFFF");
            for (int k = 0; k < GridView2.HeaderRow.Cells.Count; k++)
            {
                GridView2.HeaderRow.Cells[k].Style.Add("background-color", "#0099FF");

            }
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {

                GridViewRow row = GridView2.Rows[i];

                //Change Color back to white

                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row

                row.Attributes.Add("class", "textmode");
                for (int j = 0; j < GridView2.HeaderRow.Cells.Count; j++)
                {
                    if (i % 2 != 0)
                    {
                        row.Cells[j].Style.Add("background-color", "#33CCFF");

                    }

                }



            }

            GridView2.RenderControl(hw);

            //style to format numbers to string

            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }
        public override void VerifyRenderingInServerForm(Control control) { }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (GridView2.Visible == true)
            {
                GridView2Excal((DataTable)Session["GridViewDataSource"]);
                lb_warn.Visible = false;
            }
            else
            {
                lb_warn.Visible = true;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("ShowTime_Empoyee.aspx");
        }
    }
}